package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.module.common.network.ResponseUtils;
import com.motu.monstercity.module.common.servermanager.ServerManager;
import com.motu.monstercity.protocol.Common;
import com.motu.monstercity.protocol.Common.PBChatMsg;
import com.motu.monstercity.protocol.CsGameChat.CSGameChatMsgResponse;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.userdata.ChatPublic;
import com.motu.monstercity.server.game.userdata.UserExtInfo;
import com.motu.monstercity.server.game.userdata.UserInfo;
import com.motu.monstercity.server.game.userdata.UserShow;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// 聊天发言管理类
public class ChatManager extends ChatUserData {
    public static final Logger logger = LoggerFactory.getLogger(ChatManager.class);
    //聊天类型。-- 单服频道不允许超过100
    public static final int CHAT_TYPE_SERVER = 0;// 世界聊天(本服聊天)
    public static final int CHAT_TYPE_UNION = 1;// 联盟聊天
    public static final int CHAT_TYPE_FEAST = 3;// 派对聊天
    public static final int CHAT_TYPE_WORLD = 4;// 跨服聊天
    public static final int CHAT_TYPE_BUDDY = 9;// 好友聊天
    public static final int CHAT_TYPE_SYSTEM = 99;// 系统消息

    // 聊天子类型
    public static final int CHAT_SUB_TYPE_COMMON = 0; // 普通聊天
    public static final int CHAT_SUB_TYPE_COMMON_SYSTEM = 99;// 系统消息

    //是否已读
    public static final byte CHAT_IS_READ_NO = 0;//未读
    public static final byte CHAT_IS_READ_YES = 1;//已读


    /**
     * 获取私聊的用户拼接id
     */
    public static String getUserJointId(long userId1, long userId2) {
        if (userId1 < userId2) {
            return userId1 + "." + userId2;
        } else {
            return userId2 + "." + userId1;
        }
    }

    // 聊天推送的数据格式
    public static JsonObject takeResponseData(PBChatMsg.Builder chatBuilder) {
        CSGameChatMsgResponse.Builder respone = CSGameChatMsgResponse.newBuilder();
        respone.setChat(chatBuilder);
        return ResponseUtils.getChatResponseData(respone.build().toByteArray());// 广播给其他人的数据
    }

    /**
     * 构建聊天内容的pb数据格式
     * @param userId
     * @param id
     * @param chatType
     * @param subType
     * @param msg
     * @param addTime
     * @return
     */
    public static PBChatMsg.Builder takeChatMsgBuilder(long userId, long id, int chatType, int subType, String msg,long addTime,String param) {
        PBChatMsg.Builder builder = PBChatMsg.newBuilder();
        if(subType != ChatManager.CHAT_TYPE_SYSTEM && subType != ChatManager.CHAT_SUB_TYPE_COMMON_SYSTEM) {
            // 类型为系统消息 || 子类型为系统消息 不展示userShow
            UserShow userShow = GameUser.getUserShow(userId);
            builder.setUserShow(userShow.takeInitBuilder());
        }
        builder.setId(id);
        builder.setChatType(chatType);
        builder.setSubType(subType);
        builder.setContent(msg);
        builder.setAddTime(addTime);
        builder.setParam(param);
        // 内容有别的字段在这边新增

        return builder;
    }


    // ip是否被禁言了
    public static boolean checkForbiddenIp(String ipAddress) {
        // todo
        return false;
    }

    // 判断是否被禁言,是否可以发言,聊天
    public static boolean isCanChat(UserInfo userInfo, UserExtInfo userExtInfo) {
        long curTime = TimeUtils.getCurTime();
        if (userExtInfo.getSealTime() > curTime) {
            return false;// 被禁言
        }
        if (TimeUtils.getTimeDiff(userExtInfo.getChatTime()) < AllParam.CHAT_CD_TIME) {
            return false;// 不能频繁发言
        }
        if (checkForbiddenIp(userExtInfo.getIp())) {
            return false;//ip 被禁言
        }
        return true;
    }

    /**
     * 获取chatpublic listkey groupId
     */
    public static int getGroupId(int serverId, int chatType) {
        // 非跨服聊天类型 即本服
        if (chatType == CHAT_TYPE_SERVER || chatType == CHAT_TYPE_SYSTEM) {
            //单服聊天||//系统消息
            return chatGroupId(serverId, chatType);// 默认单服
        }
         //跨服聊天-战区分组
        if (chatType == CHAT_TYPE_WORLD) {
            return ServerManager.getWarZoneId(serverId);
        }
//        if (chatType == CHAT_TYPE_UNION) {
//            return
//        }
        // 跨服聊天类型
        return 0;
//        int group = GameDataManager.getServerGroupWarZone(serverId);
//        return chatGroupId(group, chatType);
    }

    /**
     * 拼接chatpublic的 listkey groupId
     */
    public static int chatGroupId(int group, int chatType) {
        return group * 100 + chatType;
    }

    /**
     * 获取系统消息
     */
    public static String getSystemMsg(int type, Object... params) {
        if (!GameDataManager.isSystemWordType(type)) {
            return String.valueOf(type);
        }
        JsonObject obj = new JsonObject();
        obj.put("systemWordId", type);
        if (params.length > 0) {
            JsonArray paramAry = new JsonArray();
            for (Object param : params) {
                paramAry.add(param);
            }
            obj.put("data", paramAry);
        }
        return "&&&" + obj.toString() + "&&&";
    }


}
